clear all
set more off
cap log close

********************************************************************************
***** Project: The Short and Long Term Effects of In-Person Performance Feedback
********************************************************************************
***** A. R. Soetevent & G. J. Romensen
********************************************************************************
***** Graph: Time of First Coaching
********************************************************************************
***** Latest update: 18-01-2020
********************************************************************************

*global filepath "C:\JPEMicReplication"
*global paperpath "$filepath\TablesGraphs"
log using "$filepath/Logs/09Coachingtimes.log", replace

********************************************************************************
*** DATA FRIESLAND 
********************************************************************************
use "$filepath\Data\Using databases\coachlogs.dta"

gen coachyearweek=yw(year(coachdatum_1), week(coachdatum_1))
format coachyearweek %tw

keep chauf_nr_rug coachyearweek

sort chauf_nr_rug

save "$filepath\Data\Using databases\coachyearweeks.dta", replace

clear

use "$filepath\ConstructedData\analysis_tripdata_1501 tm 1701.dta"

sort chauf_nr_rug
joinby chauf_nr_rug using "$filepath\Data\Using databases\coachyearweeks.dta", unmatched(master)
drop _merge

gen jaar=year(date(string(datum, "%td"), "DMY"))
gen week=week(date(string(datum, "%td"), "DMY"))
gen maand=month(date(string(datum, "%td"), "DMY"))
gen dag=day(date(string(datum, "%td"), "DMY"))

gen jaarweek=yw(jaar, week)
format jaarweek %tw

bysort chauf_nr_rug: gen trained_w=cond(jaarweek >= coachyearweek & coachyearweek!=.,1,0)
bysort chauf_nr_rug: gen trained_w2=cond(jaarweek == coachyearweek,1,0)

preserve

collapse (mean) trained_w trained_w2, by(chauf_nr_rug jaarweek) 

levelsof jaarweek, local(levels_trainjaarweek)

foreach x of local levels_trainjaarweek {
sum trained_w if jaarweek==`x'
local trainshare_`x'=100*r(mean)
sum trained_w2 if jaarweek==`x'
local trainshare2_`x'=100*r(mean)
}

restore 

gen trainedshare=.
gen trainedshare2=.

sum jaarweek
local maxweek=r(max)

sum jaarweek if jaar==2015 & maand==9 & dag==1
local 1sep2015=r(mean)
sum jaarweek if jaar==2015 & maand==11 & dag==1
local 1nov2015=r(mean)
sum jaarweek if jaar==2015 & maand==12 & dag==15
local 15dec2015=r(mean)
sum jaarweek if jaar==2016 & maand==11 & dag==15
local 15nov2016=r(mean)

collapse (mean) trainedshare trainedshare2, by(jaarweek)

foreach x of local levels_trainjaarweek {
replace trainedshare=`trainshare_`x'' if jaarweek==`x'
replace trainedshare2=`trainshare2_`x'' if jaarweek==`x'
}

gen empty=.

*****************************************************************************
***  [Figure D.2: Time of First Coaching]
*****************************************************************************
twoway (bar trainedshare jaarweek if inrange(jaarweek, `1sep2015',`maxweek'), xaxis(1) yaxis(1) bcolor(ltblue)) || ///
(bar trainedshare2 jaarweek if inrange(jaarweek, `1sep2015',`maxweek'), xaxis(1) yaxis(1) bcolor(navy)) || ///
(line empty jaarweek if inrange(jaarweek, `1sep2015',`maxweek'), xaxis(2) yaxis(2)), ///
xtitle("week", axis(1)) xtitle("", axis(2)) ///
xlabel(`1nov2015' "Announcement" `15nov2016' "Post PC Feedback", axis(2)) ///
tmtick(2015w35(1)2017w5)  ///
ytitle(Percentage coached drivers, axis(1)) ytitle("", axis(2)) ///
legend(order(1 2) label(1 coached (cumulative)) label(2 coached (per week)) pos(6) rows(1)) ///
graphregion(fcolor(white) lcolor(white)) bgcolor(white) ///
name(eerstekeercoaching, replace)

graph display eerstekeercoaching
graph export "$paperpath/eerstekeercoaching.png", replace name(eerstekeercoaching) 
graph save "$paperpath\eerstekeercoaching.eps", replace
graph export "$paperpath\eerstekeercoaching.eps", replace

********************************************************************************
*** DATA ZUID HOLLAND
********************************************************************************
clear all
use "$filepath\ConstructedData\analysis_ZHDAV_1507 tm 1712.dta"

/*** 1. Preliminaries ***/

tab coachidentifier

* Drop drivers who are coaches themselves from the analysis
local coachids "228 233 244 358 280 644 802"

foreach x of local coachids {
drop if driverid==`x'
}

/*** 2. Driver-specific year-week of first coaching ***/

preserve
gen coachyearweek=yw(year(coachdatum_1), week(coachdatum_1))
format coachyearweek %tw

bysort driverid: keep if _n==1

keep driverid coachyearweek

sort driverid

save "$filepath\Data\Using databases\coachyearweeksZH.dta", replace
restore

/*** 3. Determine coaching share per week and cumulative share of coaching ***/

joinby driverid using "$filepath\Data\Using databases\coachyearweeksZH.dta", unmatched(master)
drop _merge

gen year=year(date(string(datum, "%td"), "DMY"))
gen week2=week(date(string(datum, "%td"), "DMY"))


gen jaarweek=yw(year, week2)
format jaarweek %tw

drop year week2

bysort driverid: gen trained_w=cond(jaarweek >= coachyearweek & coachyearweek!=.,1,0)
bysort driverid: gen trained_w2=cond(jaarweek == coachyearweek,1,0)

preserve

collapse (mean) trained_w trained_w2, by(driverid jaarweek) 

levelsof jaarweek, local(levels_trainjaarweek)

foreach x of local levels_trainjaarweek {
sum trained_w if jaarweek==`x'
local trainshare_`x'=100*r(mean)
sum trained_w2 if jaarweek==`x'
local trainshare2_`x'=100*r(mean)
}

restore 

/*** 4. Generate figure with timing of coaching ***/

gen trainedshare=.
gen trainedshare2=.

sum jaarweek
local maxweek=r(max)

sum jaarweek if datum==date("1-12-2016", "DMY")
local 01dec2016=r(mean)

sum jaarweek if datum==date("1-9-2016", "DMY")
local 01sep2016=r(mean)

collapse (mean) trainedshare trainedshare2, by(jaarweek)

foreach x of local levels_trainjaarweek {
replace trainedshare=`trainshare_`x'' if jaarweek==`x'
replace trainedshare2=`trainshare2_`x'' if jaarweek==`x'
}

gen empty=.

twoway (bar trainedshare jaarweek if inrange(jaarweek,`01sep2016',`maxweek'), xaxis(1) yaxis(1) bcolor(ltblue)) || ///
(bar trainedshare2 jaarweek if inrange(jaarweek,`01sep2016',`maxweek'), xaxis(1) yaxis(1) bcolor(navy)) || ///
(line empty jaarweek if inrange(jaarweek,`01sep2016',`maxweek'), xaxis(2) yaxis(2)), ///
xtitle("week", axis(1)) xtitle("", axis(2)) ///
xlabel(`01dec2016' "Announcement", axis(2)) ///
tmtick(2016w35(1)2017w35)  ///
ytitle(Percentage coached drivers, axis(1)) ytitle("", axis(2)) ///
legend(order(1 2) label(1 coached (cumulative)) label(2 coached (per week)) pos(6) rows(1)) ///
graphregion(fcolor(white) lcolor(white)) bgcolor(white) ///
name(timingcoachingZH, replace)

graph display timingcoachingZH
graph export "$paperpath/timingcoachingZH.png", replace name(timingcoachingZH) 
graph save "$paperpath\timingcoachingZH.eps", replace
graph export "$paperpath\timingcoachingZH.eps", replace


log close
